﻿/**
 * @file		ct_dd_pmc_adc.c
 * @brief		PMC ADC driver Component Test
 * @note		None
 * @attention	None
 * 
 * <B><I>Copyright 2016 Socionext Inc.</I></B>
 */

#include <stdlib.h>
#include <string.h>
#include "driver_common.h"
#include "ct_dd_pmc_adc.h"
#include "dd_pmc_adc.h"
#include "pmc.h"

/*----------------------------------------------------------------------*/
/* Definition															*/
/*----------------------------------------------------------------------*/
// Nothing Special

/*----------------------------------------------------------------------*/
/* Enumeration															*/
/*----------------------------------------------------------------------*/
// Nothing Special

/*----------------------------------------------------------------------*/
/* Structure															*/
/*----------------------------------------------------------------------*/
// Nothing Special

/*----------------------------------------------------------------------*/
/* Global Data															*/
/*----------------------------------------------------------------------*/
int interrupt_count = 0;

/*----------------------------------------------------------------------*/
/* Local Method Definition												*/
/*----------------------------------------------------------------------*/
// Nothing Special

/*----------------------------------------------------------------------*/
/* Local Function														*/
/*----------------------------------------------------------------------*/
static VOID ct_dd_pmc_adc_callback(VOID);

/**
ADC callback function.
*/
static VOID ct_dd_pmc_adc_callback(VOID)
{
	if (interrupt_count >= 7) {
		interrupt_count = 0;
		Ddim_Print(("ADC Callback\n"));
		Dd_PMC_ADC_Stop();
	}
	else {
		interrupt_count++;
	}
}

/*----------------------------------------------------------------------*/
/* Global Function														*/
/*----------------------------------------------------------------------*/
/**
 * @brief  Command main function for ADC test.
 * @param  int argc	:The number of parameters
 *		   char** argv	:The value of parameters
 * @return VOID
 *
 *		The meaning of parameters
 *		+-----------+------------+-------+---------------------------------------+
 *		| P1        | P2         | P3    |  Meaning                              |
 *		+-----------+------------+-------+---------------------------------------+
 *		| init      |                    | initialization                        |
 *		+-----------+------------+-------+---------------------------------------+
 *		| ctrl      | set        | 0     | Set Control data (pattern 1)          |
 *		|           |            +-------+---------------------------------------+
 *		|           |            | 1     | Set Control data (pattern 2)          |
 *		|           |            +-------+---------------------------------------+
 *		|           |            | 2     | Set Control data (pattern 3)          |
 *		|           |            +-------+---------------------------------------+
 *		|           |            | 3     | Set Control data (Null parameter)     |
 *		|           +------------+-------+---------------------------------------+
 *		|           | get        | 0     | Get Control data                      |
 *		|           |            +-------+---------------------------------------+
 *		|           |            | 1     | Get Control data (Null parameter)     |
 *		+-----------+------------+-------+---------------------------------------+
 *		| timer     | XXXXX      |       | Set Timer (P2=cycle time[ms])         |
 *		+-----------+------------+-------+---------------------------------------+
 *		| dma       | 0xXXXXXXXX |       | Set DMA Transfer (P2=SDRAM Address)   |
 *		+-----------+------------+-------+---------------------------------------+
 *		| start     | 0          |       | Start (set callback function)         |
 *		|           +------------+-------+---------------------------------------+
 *		|           | 1          |       | Start (not set callback function)     |
 *		+-----------+------------+-------+---------------------------------------+
 *		| stop      |                    | Stop                                  |
 *		+-----------+------------+-------+---------------------------------------+
 *		| terminate |                    | Terminate                             |
 *		+-----------+------------+-------+---------------------------------------+
 *		| get       | value      | 0-7   | Get A/D data (ch0-7)                  |
 *		|           |            +-------+---------------------------------------+
 *		|           |            | 8     | Get A/D data (All ch)                 |
 *		|           |            +-------+---------------------------------------+
 *		|           |            | 9     | Get A/D data (ch0, Null parameter)    |
 *		|           |            +-------+---------------------------------------+
 *		|           |            | 10    | Get A/D data (All ch, Null parameter) |
 *		|           |            +-------+---------------------------------------+
 *		|           |            | other | Get A/D data (ch number over)         |
 *		+-----------+------------+-------+---------------------------------------+
 */                                      
VOID Ct_Dd_Pmc_Adc_Main(int argc, char** argv)
{
	INT32				ret;
	int					param2;
	int					param3;
	int					param4;
	int					loop;
	T_DD_PMC_ADC_CTRL	ctrl;
	T_DD_PMC_ADC_DATA	adc_data_list;
	USHORT				adc_data;
	ULONG				conv_cycle;
	ULONG				sdram_address;
	
	if(strcmp(argv[1], "init") == 0){
		Dd_PMC_ADC_Init();
		Ddim_Print(("Dd_PMC_ADC_Init : execute.\n"));
		// register data output.
		Ddim_Print(("--- ADC Register ---\n"));
		Ddim_Print(("ADC1  : 0x%08x\n", IO_PMC.ADC.ADC1.word));
		Ddim_Print(("ADC2  : 0x%08x\n", IO_PMC.ADC.ADC2.word));
		Ddim_Print(("ADMR  : 0x%08x\n", IO_PMC.ADC.ADMR.word));
		Ddim_Print(("ADST  : 0x%08x\n", IO_PMC.ADC.ADST.word));
		Ddim_Print(("ADCRL : 0x%08x\n", IO_PMC.ADC.ADCRL.word));
		Ddim_Print(("ADCRH : 0x%08x\n", IO_PMC.ADC.ADCRH.word));
		Ddim_Print(("--------------------\n"));
	}
	else if(strcmp(argv[1], "ctrl") == 0){
		if(strcmp(argv[2], "set") == 0){
			// get 3rd paramter.
			param3 = atoi(argv[3]);
			if (param3 == 0) {
				// set control data.
				ctrl.ch				= D_DD_PMC_ADC_CH_0;
				ctrl.cnv_mode		= E_DD_PMC_ADC_CONV_MODE_SINGLE;
				ctrl.start_trig		= E_DD_PMC_ADC_START_TRIG_SOFT;
				ctrl.timer_trig		= E_DD_PMC_ADC_TIMER_TRIG_NONE;
				ctrl.tsel			= E_DD_PMC_ADC_TSEL_INTERRUPT;
				ctrl.ext_edge		= E_DD_PMC_ADC_EXT_TRIG_EDGE_FALL;
				ctrl.sampling_time	= 0xBB;
				ctrl.powerdown		= 0;
				ctrl.cmp_data		= 0;
				// ADC Control.
				ret = Dd_PMC_ADC_Ctrl(&ctrl);
				if (ret == D_DDIM_OK) {
					Ddim_Print(("Dd_PMC_ADC_Ctrl : OK\n"));
					// register data output.
					Ddim_Print(("--- ADC Register ---\n"));
					Ddim_Print(("ADC1  : 0x%08x\n", IO_PMC.ADC.ADC1.word));
					Ddim_Print(("ADC2  : 0x%08x\n", IO_PMC.ADC.ADC2.word));
					Ddim_Print(("ADMR  : 0x%08x\n", IO_PMC.ADC.ADMR.word));
					Ddim_Print(("ADST  : 0x%08x\n", IO_PMC.ADC.ADST.word));
					Ddim_Print(("ADCRL : 0x%08x\n", IO_PMC.ADC.ADCRL.word));
					Ddim_Print(("ADCRH : 0x%08x\n", IO_PMC.ADC.ADCRH.word));
					Ddim_Print(("--------------------\n"));
				}
				else {
					Ddim_Print(("Dd_PMC_ADC_Ctrl : NG  ret=0x%08x\n", ret));
				}
			}
			else if (param3 == 1) {
				// set control data.
				ctrl.ch				= D_DD_PMC_ADC_CH_0 | D_DD_PMC_ADC_CH_1 | D_DD_PMC_ADC_CH_2 | D_DD_PMC_ADC_CH_3 | D_DD_PMC_ADC_CH_4 | D_DD_PMC_ADC_CH_5 | D_DD_PMC_ADC_CH_6 | D_DD_PMC_ADC_CH_7;
				ctrl.cnv_mode		= E_DD_PMC_ADC_CONV_MODE_CONT_LOW;
				ctrl.start_trig		= E_DD_PMC_ADC_START_TRIG_BOTH;
				ctrl.timer_trig		= E_DD_PMC_ADC_TIMER_TRIG_0;
				ctrl.tsel			= E_DD_PMC_ADC_TSEL_TOGGLE;
				ctrl.ext_edge		= E_DD_PMC_ADC_EXT_TRIG_EDGE_FALL;
				ctrl.sampling_time	= 0x80;
				ctrl.powerdown		= 1;
				ctrl.cmp_data		= 0x3FF;
				// ADC Control.
				ret = Dd_PMC_ADC_Ctrl(&ctrl);
				if (ret == D_DDIM_OK) {
					Ddim_Print(("Dd_PMC_ADC_Ctrl : OK\n"));
					// register data output.
					Ddim_Print(("--- ADC Register ---\n"));
					Ddim_Print(("ADC1  : 0x%08x\n", IO_PMC.ADC.ADC1.word));
					Ddim_Print(("ADC2  : 0x%08x\n", IO_PMC.ADC.ADC2.word));
					Ddim_Print(("ADMR  : 0x%08x\n", IO_PMC.ADC.ADMR.word));
					Ddim_Print(("ADST  : 0x%08x\n", IO_PMC.ADC.ADST.word));
					Ddim_Print(("ADCRL : 0x%08x\n", IO_PMC.ADC.ADCRL.word));
					Ddim_Print(("ADCRH : 0x%08x\n", IO_PMC.ADC.ADCRH.word));
					Ddim_Print(("--------------------\n"));
				}
				else {
					Ddim_Print(("Dd_PMC_ADC_Ctrl : NG  ret=0x%08x\n", ret));
				}
			}
			else if (param3 == 2) {
				// set control data.
				ctrl.ch				= D_DD_PMC_ADC_CH_0 | D_DD_PMC_ADC_CH_1 | D_DD_PMC_ADC_CH_2 | D_DD_PMC_ADC_CH_3 | D_DD_PMC_ADC_CH_4 | D_DD_PMC_ADC_CH_5 | D_DD_PMC_ADC_CH_6 | D_DD_PMC_ADC_CH_7;
				ctrl.cnv_mode		= E_DD_PMC_ADC_CONV_MODE_SINGLE;
				ctrl.start_trig		= E_DD_PMC_ADC_START_TRIG_TIMER;
				ctrl.timer_trig		= E_DD_PMC_ADC_TIMER_TRIG_0;
				ctrl.tsel			= E_DD_PMC_ADC_TSEL_INTERRUPT;
				ctrl.ext_edge		= E_DD_PMC_ADC_EXT_TRIG_EDGE_FALL;
				ctrl.sampling_time	= 0x40;
				ctrl.powerdown		= 0;
				ctrl.cmp_data		= 0x1FF;
				// ADC Control.
				ret = Dd_PMC_ADC_Ctrl(&ctrl);
				if (ret == D_DDIM_OK) {
					Ddim_Print(("Dd_PMC_ADC_Ctrl : OK\n"));
					// register data output.
					Ddim_Print(("--- ADC Register ---\n"));
					Ddim_Print(("ADC1  : 0x%08x\n", IO_PMC.ADC.ADC1.word));
					Ddim_Print(("ADC2  : 0x%08x\n", IO_PMC.ADC.ADC2.word));
					Ddim_Print(("ADMR  : 0x%08x\n", IO_PMC.ADC.ADMR.word));
					Ddim_Print(("ADST  : 0x%08x\n", IO_PMC.ADC.ADST.word));
					Ddim_Print(("ADCRL : 0x%08x\n", IO_PMC.ADC.ADCRL.word));
					Ddim_Print(("ADCRH : 0x%08x\n", IO_PMC.ADC.ADCRH.word));
					Ddim_Print(("--------------------\n"));
				}
				else {
					Ddim_Print(("Dd_PMC_ADC_Ctrl : NG  ret=0x%08x\n", ret));
				}
			}
			else if (param3 == 3) {
				// input paramter error.
				ret = Dd_PMC_ADC_Ctrl(NULL);
				Ddim_Print(("Dd_PMC_ADC_Ctrl : NG  ret=0x%08x\n", ret));
			}
			else if (param3 == 4) {
				param4 = atoi(argv[4]);
				
				SHORT ch = 0;
				
				switch( param4 ) {
					case 0:
						ch = D_DD_PMC_ADC_CH_0;
						break;
					case 1:
						ch = D_DD_PMC_ADC_CH_1;
						break;
					case 2:
						ch = D_DD_PMC_ADC_CH_2;
						break;
					case 3:
						ch = D_DD_PMC_ADC_CH_3;
						break;
					case 4:
						ch = D_DD_PMC_ADC_CH_4;
						break;
					case 5:
						ch = D_DD_PMC_ADC_CH_5;
						break;
					case 6:
						ch = D_DD_PMC_ADC_CH_6;
						break;
					case 7:
						ch = D_DD_PMC_ADC_CH_7;
						break;
					default:
						Ddim_Print(("please check 4th parameter!!\n"));
						return;
				}
				
				// set control data.
				ctrl.ch				= ch;
				ctrl.cnv_mode		= E_DD_PMC_ADC_CONV_MODE_SINGLE;
				ctrl.start_trig		= E_DD_PMC_ADC_START_TRIG_TIMER;
				ctrl.timer_trig		= E_DD_PMC_ADC_TIMER_TRIG_0;
				ctrl.tsel			= E_DD_PMC_ADC_TSEL_INTERRUPT;
				ctrl.ext_edge		= E_DD_PMC_ADC_EXT_TRIG_EDGE_FALL;
				ctrl.sampling_time	= 0x40;
				ctrl.powerdown		= 0;
				ctrl.cmp_data		= 0x1FF;
				// ADC Control.
				ret = Dd_PMC_ADC_Ctrl(&ctrl);
				if (ret == D_DDIM_OK) {
					Ddim_Print(("Dd_PMC_ADC_Ctrl : OK\n"));
					// register data output.
					Ddim_Print(("--- ADC Register ---\n"));
					Ddim_Print(("ADC1  : 0x%08x\n", IO_PMC.ADC.ADC1.word));
					Ddim_Print(("ADC2  : 0x%08x\n", IO_PMC.ADC.ADC2.word));
					Ddim_Print(("ADMR  : 0x%08x\n", IO_PMC.ADC.ADMR.word));
					Ddim_Print(("ADST  : 0x%08x\n", IO_PMC.ADC.ADST.word));
					Ddim_Print(("ADCRL : 0x%08x\n", IO_PMC.ADC.ADCRL.word));
					Ddim_Print(("ADCRH : 0x%08x\n", IO_PMC.ADC.ADCRH.word));
					Ddim_Print(("--------------------\n"));
				}
				else {
					Ddim_Print(("Dd_PMC_ADC_Ctrl : NG  ret=0x%08x\n", ret));
				}
			}
			else {
				Ddim_Print(("please check 3rd parameter!!\n"));
			}
		}
		else if(strcmp(argv[2], "get") == 0){
			// get 3rd paramter.
			param3 = atoi(argv[3]);
			if (param3 == 0) {
				// control data clear.
				memset(&ctrl, 0, sizeof(T_DD_PMC_ADC_CTRL));
				// get control data.
				ret = Dd_PMC_ADC_Get_Ctrl(&ctrl);
				if (ret == D_DDIM_OK) {
					Ddim_Print(("Dd_PMC_ADC_Get_Ctrl : OK\n"));
					// register data output.
					Ddim_Print(("--- ADC Register ---\n"));
					Ddim_Print(("ADC1  : 0x%08x\n", IO_PMC.ADC.ADC1.word));
					Ddim_Print(("ADC2  : 0x%08x\n", IO_PMC.ADC.ADC2.word));
					Ddim_Print(("ADMR  : 0x%08x\n", IO_PMC.ADC.ADMR.word));
					Ddim_Print(("ADST  : 0x%08x\n", IO_PMC.ADC.ADST.word));
					Ddim_Print(("ADCRL : 0x%08x\n", IO_PMC.ADC.ADCRL.word));
					Ddim_Print(("ADCRH : 0x%08x\n", IO_PMC.ADC.ADCRH.word));
					// get data output.
					Ddim_Print(("--- Get Data -------\n"));
					Ddim_Print(("ch            : 0x%04x\n", ctrl.ch));
					Ddim_Print(("cnv_mode      : %d\n", ctrl.cnv_mode));
					Ddim_Print(("start_trig    : %d\n", ctrl.start_trig));
					Ddim_Print(("timer_trig    : %d\n", ctrl.timer_trig));
					Ddim_Print(("tsel          : %d\n", ctrl.tsel));
					Ddim_Print(("ext_edge      : %d\n", ctrl.ext_edge));
					Ddim_Print(("sampling_time : 0x%02x\n", ctrl.sampling_time));
					Ddim_Print(("powerdown     : %d\n", ctrl.powerdown));
					Ddim_Print(("cmp_data      : 0x%04x\n", ctrl.cmp_data));
					Ddim_Print(("--------------------\n"));
				}
				else {
					Ddim_Print(("Dd_PMC_ADC_Get_Ctrl : NG  ret=0x%08x\n", ret));
				}
			}
			else if (param3 == 1) {
				// input paramter error.
				ret = Dd_PMC_ADC_Get_Ctrl(NULL);
				Ddim_Print(("Dd_PMC_ADC_Get_Ctrl : NG  ret=0x%08x\n", ret));
			}
			else {
				Ddim_Print(("please check 3rd parameter!!\n"));
			}
		}
		else {
			Ddim_Print(("please check 2nd parameter!!\n"));
		}
	}
	else if(strcmp(argv[1], "timer") == 0){
		// get 2nd paramter.
		conv_cycle = strtoul(argv[2], NULL, 0);
		ret = Dd_PMC_ADC_Set_Timer(conv_cycle);								/* pgr0539 */
		if (ret == D_DDIM_OK) {
			Ddim_Print(("Dd_PMC_ADC_Set_Timer : OK\n"));
		}
		else {
			Ddim_Print(("Dd_PMC_ADC_Set_Timer : NG  ret=0x%08x\n", ret));
		}
	}
	else if(strcmp(argv[1], "dma") == 0){
		// get 2nd paramter.
		sdram_address = strtoul(argv[2], NULL, 0);
		ret = Dd_PMC_ADC_Set_DMA_Transfer(sdram_address);					/* pgr0539 */
		if (ret == D_DDIM_OK) {
			Ddim_Print(("Dd_PMC_ADC_Set_DMA_Transfer : OK\n"));
		}
		else {
			Ddim_Print(("Dd_PMC_ADC_Set_DMA_Transfer : NG  ret=0x%08x\n", ret));
		}
	}
	else if(strcmp(argv[1], "start") == 0){
		// get 2nd paramter.
		param2 = atoi(argv[2]);
		if (param2 == 0) {
			Dd_PMC_ADC_Start((VP_CALLBACK)ct_dd_pmc_adc_callback);
			Ddim_Print(("Dd_PMC_ADC_Start : set callback.\n"));
			// register data output.
			Ddim_Print(("--- ADC Register ---\n"));
			Ddim_Print(("ADC1  : 0x%08x\n", IO_PMC.ADC.ADC1.word));
			Ddim_Print(("ADC2  : 0x%08x\n", IO_PMC.ADC.ADC2.word));
			Ddim_Print(("--------------------\n"));
		}
		else if (param2 == 1) {
			Dd_PMC_ADC_Start(NULL);
			Ddim_Print(("Dd_PMC_ADC_Start : non callback.\n"));
			// register data output.
			Ddim_Print(("--- ADC Register ---\n"));
			Ddim_Print(("ADC1  : 0x%08x\n", IO_PMC.ADC.ADC1.word));
			Ddim_Print(("ADC2  : 0x%08x\n", IO_PMC.ADC.ADC2.word));
			Ddim_Print(("--------------------\n"));
		}
		else {
			Ddim_Print(("please check 2nd parameter!!\n"));
		}
	}
	else if(strcmp(argv[1], "stop") == 0){
		// Stop.
		Dd_PMC_ADC_Stop();
		Ddim_Print(("Dd_PMC_ADC_Stop : execute.\n"));
		// register data output.
		Ddim_Print(("--- ADC Register ---\n"));
		Ddim_Print(("ADC1  : 0x%08x\n", IO_PMC.ADC.ADC1.word));
		Ddim_Print(("--------------------\n"));
	}
	else if(strcmp(argv[1], "terminate") == 0){
		// Terminate.
		Dd_PMC_ADC_Terminate();
		Ddim_Print(("Dd_PMC_ADC_Terminate : execute.\n"));
		// register data output.
		Ddim_Print(("--- ADC Register ---\n"));
		Ddim_Print(("ADC1  : 0x%08x\n", IO_PMC.ADC.ADC1.word));
		Ddim_Print(("ADC2  : 0x%08x\n", IO_PMC.ADC.ADC2.word));
		Ddim_Print(("ADMR  : 0x%08x\n", IO_PMC.ADC.ADMR.word));
		Ddim_Print(("ADST  : 0x%08x\n", IO_PMC.ADC.ADST.word));
		Ddim_Print(("--------------------\n"));
	}
	else if(strcmp(argv[1], "get") == 0){
		if(strcmp(argv[2], "value") == 0){
			// get 3rd paramter.
			param3 = atoi(argv[3]);
			if ((param3 >= 0) && (param3 < D_DD_PMC_ADC_NUM)) {
				// A/D data clear.
				adc_data = 0;
				// get A/D data.
				Dd_PMC_ADC_Get_Value_By_Ch(param3, &adc_data);
				Ddim_Print(("Dd_PMC_ADC_Get_Value_By_Ch : execute.\n"));
				Ddim_Print(("  Ch = %d, Value = 0x%04x\n", param3, adc_data));
			}
			else if (param3 == D_DD_PMC_ADC_NUM) {
				// A/D data clear.
				memset(&adc_data_list, 0, sizeof(T_DD_PMC_ADC_DATA));
				// get A/D data (all channel).
				Dd_PMC_ADC_Get_Value(&adc_data_list);
				Ddim_Print(("Dd_PMC_ADC_Get_Value : execute.\n"));
				for (loop = 0; loop < D_DD_PMC_ADC_NUM; loop++) {
					Ddim_Print(("  Ch = %d, Value = 0x%04x\n", loop, adc_data_list.data[loop]));
				}
			}
			else if (param3 == D_DD_PMC_ADC_NUM + 1) {
				// Input paramter error : 1st parameter.
				Dd_PMC_ADC_Get_Value_By_Ch(0, NULL);
				Ddim_Print(("Dd_PMC_ADC_Get_Value_By_Ch : NG.\n"));
			}
			else if (param3 == D_DD_PMC_ADC_NUM + 2) {
				// Input paramter error.
				Dd_PMC_ADC_Get_Value(NULL);
				Ddim_Print(("Dd_PMC_ADC_Get_Value : NG.\n"));
			}
			else{
				// Input paramter error : 2nd parameter.
				Dd_PMC_ADC_Get_Value_By_Ch(param3, &adc_data);
				Ddim_Print(("Dd_PMC_ADC_Get_Value_By_Ch : NG.\n"));
			}
		}
		else{
			Ddim_Print(("please check 2nd parameter!!\n"));
		}
	}
	else{
		Ddim_Print(("please check 1st parameter!!\n"));
	}
}
